<?php 

class ContentController extends CommonController{
	private $model;

	public function __auto() {
		$this -> model = K('Shop');
	}
	public function index(){
		
//		p($_SESSION);
		
//		给页面分配各个表中的数据
		$gid = Q('get.gid',0,'intval');
		
		
		
		//获得顶级分类
		$cateData =K('Cate')->where("pid=0")->all();
		$this->assign("cateData",$cateData);
//		
		//第一张表
		$shopData = $this->model->where("gid={$gid}")->find();
//		p($shopData);
		$this->assign('shopData',$shopData);
		
		
		
		//循环图片
		$detail = K('Detail')->where("goods_gid={$gid}")->find();
		$pic = unserialize($detail['shop_pic']);
		$detail['shop_pic'] = $pic ;
//		p($detail);
		foreach ($detail['shop_pic'] as $k => $v) {
			//获得类型
			$typpe=strrchr($v, '.');
//			p($typpe);
			//小图
			$smallPath=str_replace($typpe, '_small'.$typpe,$v);
			//中图
			$midPath=str_replace($typpe, '_mid'.$typpe,$v);
			//大图
			$bigPath=str_replace($typpe, '_big'.$typpe,$v);
			
			$detail['shop_pic'][$k]=array(
				'small'=>$smallPath,
				'mid'=>$midPath,
				'big'=>$bigPath,
			);
			
		}
//		p($detail);
		$this->assign('detail',$detail);		
		
		
		//老师重组的数组   **********************************************五星级提示
		//***************
		$sepcData = M()->join('__type_attr__ ta JOIN __goods_attr__ ga ON ta.idtype_attr=ga.taid')->where("goods_gid={$gid} AND class=1")->all();
		$specTemp = array();
		foreach ($sepcData as $v) {
			$specTemp[$v['taid']][] = $v;
		}
//		p($specTemp);
		$tempSpec = array();
		foreach ($specTemp as $taid => $value) {
			$tempSpec[] = array(
				'name' => $value[0]['name'],
				'value' => $value
			);
		}
//		p($tempSpec);
		$this->assign('tempSpec',$tempSpec);
		//****************
		
		
		
		$this->display();
	}
	
	/**
	 * 异步获得商品价格信息
	 */
	public function getAdd(){
//		p($_POST);die;
	    if(IS_AJAX){
	    	$combine = rtrim($_POST['id'],',');
			$gid = $_POST['gid'];
//			p($combine);
			//商品的信息
			$Goodinfo = K('List')->where("goods_gid={$gid} AND combine like '{$combine}'")->find();
//			p($Goodinfo);
			//附加价格
			$combine = explode(',', $combine);
			$specArr = array();
			foreach ($combine as $v) {
				$specArr[] = K('Goods_Attr')->where("goods_gid={$gid} AND gtid={$v}")->getField('added');
			}
			$num = K('Shop')->where("gid={$gid}")->getField('shopprice');
			$specArr = array_sum($specArr);
//			p($specArr);
			$Goodinfo['specArr'] = $num+$specArr;
//			p($Goodinfo);
			//最后价格
			echo json_encode($Goodinfo);
			exit;
	    }
	}
	

	
	
	
	
	
	
}





 ?>